package com.ant.storage.service.impl;

import com.ant.storage.common.ResultCode;
import com.ant.storage.common.ResultPage;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.AllocationWarehouseEnterMapper;
import com.ant.storage.pojo.AllocationWarehouseEnter;
import com.ant.storage.service.IAllocationWarehouseEnterService;
import com.ant.storage.service.ICityService;
import com.ant.storage.service.IInventoryService;
import com.ant.storage.utils.NullInspectUtil;
import com.ant.storage.common.PageRecv;
import com.ant.storage.common.ResultVo;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AllocationWarehouseEnterServiceImpl extends ServiceImpl<AllocationWarehouseEnterMapper,AllocationWarehouseEnter> implements IAllocationWarehouseEnterService{

    @Autowired(required = false)
    AllocationWarehouseEnterMapper allocationWarehouseEnterMapper;

    @Autowired
    private ICityService cityService;

    @Autowired
    private IInventoryService inventoryService;

    @Override
    public ResultPage queryAllocationWarehouseEnter(PageRecv<AllocationWarehouseEnter> pageRecv) {
        Map map = new HashMap<>();
        if (!(pageRecv.getData()==null)){
            map = NullInspectUtil.ClassChangeMapNotNull(pageRecv.getData());
        }
        Integer page = (pageRecv.getCurrentPage()-1)*pageRecv.getPageSize();
        Integer limit = pageRecv.getCurrentPage()*pageRecv.getPageSize();
        List<AllocationWarehouseEnter> list = allocationWarehouseEnterMapper.queryAllocationWarehouseEnter(page,limit,map);
        for (AllocationWarehouseEnter allocationWarehouseEnter:list) {
            allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getSupply_city().setName(cityService.idQuery(allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocation_supply_city_id()));
            allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getSupply_dept().setName(cityService.idQuery(allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocation_supply_dept_id()));
            allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getExtract_city().setName(cityService.idQuery(allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocation_extract_city_id()));
            allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getExtract_dept().setName(cityService.idQuery(allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocation_extract_dept_id()));
        }
        Integer count = allocationWarehouseEnterMapper.countAllocationWarehouseEnter(map);
        return ResultPage.success("查询成功",list,count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo allocationWarehouseEnterEdit(AllocationWarehouseEnter allocationWarehouseEnter, int i) {
        for (int a = 0;a<allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocationAndCommodity().size();a++) {
            if(allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocationAndCommodity().get(a).getCommodity_enter_quantity() == null){
                continue;
            }
            if (!(0<allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocationAndCommodity().get(a).getCommodity_enter_quantity())&&!(allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocationAndCommodity().get(a).getCommodity_enter_quantity()<allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocationAndCommodity().get(a).getCommodity_out_quantity())){
                throw new AntException(ResultCode.ALLOCATION_OUT_NUMBER);
            }
            allocationWarehouseEnterMapper.updateAllocationAndCommodityEnter(allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocation_id(),allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocationAndCommodity().get(a).getCommodity().getCommodityId(),allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocationAndCommodity().get(a).getCommodity_enter_quantity(),allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocationAndCommodity().get(a).getEnter_warehouse_id());
        }
        allocationWarehouseEnter.setAllocation_warehouse_enter_type(i);
        UpdateWrapper<AllocationWarehouseEnter> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("allocation_warehouse_enter_id",allocationWarehouseEnter.getAllocation_warehouse_enter_id());
        this.update(allocationWarehouseEnter,updateWrapper);
        return ResultVo.success("ok");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stockAdd(AllocationWarehouseEnter allocationWarehouseEnter) {
        for(int a = 0;a<allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocationAndCommodity().size();a++){
            inventoryService.stockAdd(allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocationAndCommodity().get(a).getEnter_warehouse_id(),allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocationAndCommodity().get(a).getCommodity().getCommodityId(),allocationWarehouseEnter.getAllocationWarehouseOut().getAllocation().getAllocationAndCommodity().get(a).getCommodity_enter_quantity());
        }
    }

}
